package cn.wolfcode.service.impl;

import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.ActivityItemMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityItemServiceImpl implements IActivityItemService {

    @Autowired
    private ActivityItemMapper activityItemMapper;


    @Override
    public void save(ActivityItem activityItem) {
        activityItemMapper.insert(activityItem);
    }

    @Override
    public void delete(Long id) {
        activityItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(ActivityItem activityItem) {
        activityItemMapper.updateByPrimaryKey(activityItem);
    }

    @Override
    public ActivityItem get(Long id) {
        return activityItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ActivityItem> listAll(Long id) {
        return activityItemMapper.selectAll(id);
    }

    @Override
    public PageInfo<ActivityItem> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<ActivityItem> activityItems = activityItemMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<ActivityItem>(activityItems);
    }

    //插入关系
    public void insertToChoice(Long empId, Long optionsRadios, Date date,Long id) {
        //返回是 integer 类型,判断用户是否为空 ,为空则抛异常 ,不存在
        Integer aa =  activityItemMapper.checkPl(empId,id);
        if(aa != null){
            throw new LogicException("已经投过票");
        }
        activityItemMapper.insertToChoice(empId,optionsRadios,date);
    }


    //查询id为某一个值的所有用户
    public Integer findSum(Long id){
        return activityItemMapper.findSum(id);
    }

    @Override
    public void insert() {

    }


    //修改用户
    public void updateRelation(Long optionsRadios) {
        activityItemMapper.updateRelation(optionsRadios);
    }

    //修改状态
    public void updateStatusById(Long id, Boolean status) {
        activityItemMapper.updateStatusById(id,status);
    }


}
